package com.wlscale.roadsystem.modular.setting.syscalibration;

import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.View;
import android.widget.TextView;

import com.easy.component.component.header.EasyHeaderBuilder;
import com.wlscale.roadsystem.R;
import com.wlscale.roadsystem.base.LZBaseActivity;
import com.wlscale.roadsystem.modular.setting.syscalibration.adapter.SystemCalibrationTablePageAdapter;
import com.wlscale.roadsystem.modular.setting.syscalibration.model.SystemCalibrationModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统校准
 */
public class SystemCalibrationActivity extends LZBaseActivity {
    /**
     *
     */
    private EasyHeaderBuilder easyHeaderBuilder;
    /**
     * table
     */
    private TabLayout mTabLayout;
    /**
     * viewpage
     */
    private ViewPager mViewPager;
    /**
     * table适配器
     */
    private SystemCalibrationTablePageAdapter myFragmentPagerAdapter;
    /**
     * table数据
     */
    private List<SystemCalibrationModel> tableList = new ArrayList<>();
    @Override
    public void initlayoutResID() {
        setContentView(R.layout.activity_system_calibration);
    }

    @Override
    public void initBundle() {

    }

    @Override
    public void initView() {
        mTabLayout = findViewById(R.id.tab_order);
        mViewPager = findViewById(R.id.vp_goods);
        initTabEvent();
    }

    @Override
    public void initHeader() {
        easyHeaderBuilder = new EasyHeaderBuilder(this);
        easyHeaderBuilder.addLeftImageBackView();
        easyHeaderBuilder.setTitle("系统校准");
        easyHeaderBuilder.addLeftImageBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishActivity();
            }
        });
    }

    @Override
    public void initLoadingView() {

    }

    @Override
    public void initData() {

    }

    /**
     * 设置TabLayout的监听事件
     */
    private void initTabEvent() {
        SystemCalibrationModel tableModel = new SystemCalibrationModel();
        tableModel.setId("0");
        tableModel.setName("左传感器");
        tableList.add(tableModel);
        SystemCalibrationModel tableModel1 = new SystemCalibrationModel();
        tableModel1.setId("1");
        tableModel1.setName("右");
        tableList.add(tableModel1);


        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (null != myFragmentPagerAdapter) {
                    // 点击item
                    View customView = tab.getCustomView();
                    if (null != customView) {
                        customView.findViewById(R.id.v_indexTab).setVisibility(View.VISIBLE);
                        ((TextView) customView.findViewById(R.id.tv_tab_title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                    }
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View customView = tab.getCustomView();
                if (null != customView) {
                    customView.findViewById(R.id.v_indexTab).setVisibility(View.INVISIBLE);
                    ((TextView) customView.findViewById(R.id.tv_tab_title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        setTabs();
    }

    /**
     * 设置TabLayout
     */
    private void setTabs() {
        // 加载tab页面
        myFragmentPagerAdapter = new SystemCalibrationTablePageAdapter(this, getFragmentManager());
        for (SystemCalibrationModel model:tableList){
            String id = model.getId();
            if ("0".equals(id)){
                myFragmentPagerAdapter.addTab(new SystemCalibrationLeftFragment());
            }else {
                myFragmentPagerAdapter.addTab(new SystemCalibrationRigthFragment());
            }
        }
        //
        mViewPager.setAdapter(myFragmentPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager, true);

        //设置TabLayout TabItem
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            mTabLayout.getTabAt(i).setCustomView(myFragmentPagerAdapter.getTabView(this, tableList.get(i).getName()));
        }
        // 设置第一个颜色为蓝色
        ((View) mTabLayout.getTabAt(0).getCustomView().findViewById(R.id.v_indexTab)).setVisibility(View.VISIBLE);
        for (int i = 0; i < mTabLayout.getTabCount(); i++) {
            if (i == 0) {
                ((TextView) mTabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            } else {
                ((TextView) mTabLayout.getTabAt(i).getCustomView().findViewById(R.id.tv_tab_title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
            }
        }

    }
}
